package vn.gameloft.egg;

import java.util.ArrayList;
import java.util.List;

import vn.gameloft.egg.model.Basket;
import vn.gameloft.egg.model.Chick;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;

public class MainScreen extends ScreenAdapter {
    public static final int SCREEN_WIDTH = Gdx.graphics.getWidth();
    public static final int SCREEN_HEIGHT = Gdx.graphics.getHeight();

    private static final float CHICK_LINE = SCREEN_HEIGHT - 100;
    private static final float CHICK_DST = SCREEN_WIDTH / 3;
    private EggGame game;
    private List<Chick> chickList = new ArrayList<Chick>();
    private Basket ro;

    BitmapFont score;

    private ShapeRenderer shapeRenderer = new ShapeRenderer();

    public MainScreen(EggGame game) {
        score = new BitmapFont();
        score.setColor(Color.BLUE);
        score.setUseIntegerPositions(true);

        this.game = game;
        ro = new Basket();
        ro.x = (SCREEN_WIDTH - ro.getWidth()) / 2;
        ro.y = 10;
        Gdx.graphics.getGL20().glClearColor(0, 0, 0, 1);
        Gdx.graphics.setDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, false);
    }

    @Override
    public void resize(int width, int height) {
        super.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
    }

    @Override
    public void render(float delta) {
        Gdx.graphics.getGL20().glClear(
                GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        if (chickList.size() < 3) {
            if (Gdx.input.isKeyPressed(Input.Keys.G)) {
                addChick(0);
            } else if (Gdx.input.isKeyPressed(Input.Keys.V)) {
                addChick(1);
            } else if (Gdx.input.isKeyPressed(Input.Keys.N)) {
                addChick(2);
            } else if (Gdx.input.justTouched()) {
                addChick((int) Math.floor(Math.random() * 3));
            }
        }
        ro.x += Gdx.input.getDeltaX();

        game.batch.begin();
        for (Chick chick : chickList) {
            chick.redraw(game.batch, delta);
        }
        ro.draw(game.batch);
        score.draw(game.batch, String.valueOf(ro.point), 10f, SCREEN_HEIGHT
                - score.getCapHeight());
        game.batch.end();

        shapeRenderer.begin(ShapeType.Line);
        shapeRenderer.setColor(Color.WHITE);
        shapeRenderer.line(0, CHICK_LINE, SCREEN_WIDTH, CHICK_LINE);
        shapeRenderer.end();
    }

    private void addChick(int type) {
        Chick g;
        if (type < 1) {
            g = new Chick(Gdx.files.internal("ga.png"));
        } else if (type < 2) {
            g = new Chick(Gdx.files.internal("vit.png"));
        } else {
            g = new Chick(Gdx.files.internal("ngan.png"));
        }
        g.x = CHICK_DST * chickList.size() + (CHICK_DST - g.getWidth()) / 2;
        g.y = CHICK_LINE;
        g.ro = this.ro;
        chickList.add(g);
    }
}
